/**
 * 窗口遮罩，阻止操作层
 */

/**
 * 显示或隐藏遮罩层
 * 
 * @param status "show" or "hide"
 * @param index 指定遮罩层的 zIndex
 */

( function( $ )
{

	$.overlay = {
		id: "__POPUP_OVERLAY__",
		resize: true, // re-centers the dialog on window resize
		opacity: .1, // transparency level of overlay
		color: '#000', // base color of overlay

		hide: function()
		{
			$.overlay.show( false );
			$.overlay.maintainPosition( false );
		},

		show: function( status, index, id )
		{
			if( isNaN( index ) )
				index = 0;

			if( empty( id ) )
				id = $.overlay.id;

			if( status )
			{
				$( "#" + id ).remove();
				$( "body" ).append( '<div id="' + id + '"></div>' );
				$( "#" + id ).css(
						{ position: 'fixed', zIndex: index, top: '0px', left: '0px',
							background: $.overlay.color, opacity: $.overlay.opacity } );

				// 初始化遮罩层位置及大小
				$.overlay.reposition();
				$.overlay.maintainPosition( true );
			}
			else
			{
				$( "#" + id ).remove();
			}
		},

		reposition: function()
		{
			var layer = $( "#" + $.overlay.id );
			layer.size( $( window ).width(), $( window ).height() );
			layer.move( 0, 0 );
		},

		maintainPosition: function( status )
		{
			if( $.overlay.resize )
			{
				switch( status ) {
					case true:
						$( window ).bind( 'resize', $.overlay.reposition );
						break;
					case false:
						$( window ).unbind( 'resize', $.overlay.reposition );
						break;
				}
			}
		}

	};

	// Shortuct functions
	overlay = function( status, index )
	{
		$.overlay.show( status, index );
	};

} )( jQuery );
